# -*- coding: utf-8 -*-
# __author__ = 'yeslf'
# env: ptyon 2.7 etcd 3.4.22 etcd3==0.12.0 bottle bottle 0.12.23
# fuction: 1、输出 etcd 所有的 key
#          2、模糊匹配 查询 etcd 的 key
#          3、查询 单个 etcd 指定key 的值


from __future__ import unicode_literals, print_function

import etcd3,bottle

from bottle  import route,run,template,request

# k8s etcd use
#client=etcd3.client(host='127.0.0.1', port=2379, ca_cert="/etc/kubernetes/pki/etcd/ca.crt", cert_key="/etc/kubernetes/pki/etcd/server.key", cert_cert="/etc/kubernetes/pki/etcd/server.crt")

#client=etcd3.client(host='192.168.55.51')
client=etcd3.client(host='192.168.55.51')

indexuse= '''
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<body>

<h3>表</h3>
<h3>序号------key</h3>

1 <a href="/allkey">全key</a><br>
2 <a href="/selfkeyinput">模糊匹配key</a><br>
3 <a href="/getvalueinput">查询指定key的值</a><br>

</body>

</html>

'''

keylist='''
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<body>

         <form action="/selfallkey" method="POST">

          模糊过滤key:<input type="text" name="regkey">

          <input type="submit" name="submit" >	

</from>
<hr>

<h3>显示列表</h3>
<h3>序号------key</h3>

% for i in showlist:
    
    <h5> {{showlist.index(i)+1}} {{i}} </h5>
% end


</body>

</html>

'''

regkeylist = '''
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<body>

         <form action="/selfallkey" method="POST">

          模糊过滤key:<input type="text" name="regkey">

          <input type="submit" name="submit">	

</from>

<hr>

<h3>显示列表</h3>
<h3>序号------key</h3>

</body>

</html>

'''

getetcdkey='''
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<body>

<form action="/getvalue" method = "POST">

etcdkey: <input type="text" name="key" >

<input  type="submit" name="submit" >

</from>
<hr>

<h3>etcd 取的值</h3>
    
    <h5>  {{ getvalue}} </h5>

<a href="/allkey">keyall</a>
</body>

</html>

'''



@route('/')
def index():

    return template(indexuse)

@route('/selfkeyinput')
def selfkeyinput():
    return template(regkeylist, showlist=None)


@route('/allkey')
@route('/selfallkey',method='post')
def showallkey():
    print("获取当前 etcd 中 所有的key ")

    getallkey=request.forms.get('regkey')

    print(getallkey)

    if getallkey==None or getallkey=='':
      showlist = []
      getshowlist = None
      getshowlist=client.get_prefix(key_prefix='/', keys_only=True)
      print("to if")

      for i in getshowlist:

        #print(i[1].key)
        showlist.append(i[1].key)

      #return '''所有key'''
      return template(keylist, showlist=showlist)

    else:
      showlist = []
      getshowlist = None
      print("ssssssssssssssssssss")
      print(str(getallkey).strip())
      getshowlist=client.get_prefix(key_prefix=str(getallkey).strip(),keys_only=True)

      for i in getshowlist:

        #print(i[1].key)
        showlist.append(i[1].key)


      return template(keylist, showlist=showlist)

@route('/getvalueinput')
def getvalueinput():

    return template(getetcdkey,getvalue=None)


@route('/getvalue',method='post')
def getvalue():
    etcdkey = str(request.forms.get('key')).strip()

    if etcdkey==None or etcdkey=='':
      template(getetcdkey,getvalue=None)

    else:

      print(etcdkey)
      getvalue = client.get(etcdkey)

      print("当前获取到的values")
      print(getvalue)

      return template(getetcdkey,getvalue=getvalue)


if __name__ == '__main__':
    run(host='0.0.0.0', port=6080, reloader=True, debug=True)

else:
    application = bottle.defaul_app()

